package fdoj.eventInvitations.dao;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NoResultException;
import javax.persistence.Query;

import fdoj.eventInvitations.entity.Person;
/**
 * Klasa koja implementira CRUD operacije za entitet {@link Person} 
 */
@Stateless
@Local(PersonDao.class)
public class PersonDaoBean extends GenericDaoBean<Person, Integer> implements
		PersonDao {

	public Person findByEmail(String email) {
		Person retVal = null;
		try {
			Query q = em.createQuery("SELECT x FROM Person x WHERE x.email like :email ");
			q.setParameter("email", email);
			retVal= (Person) q.getSingleResult();
		} catch (NoResultException ex) {

		}
		return retVal;
	}
	@SuppressWarnings("unchecked")
	  public Person login(String email, String password) {
	    Query q = em.createQuery(
	        "SELECT p FROM Person p WHERE p.email=:email AND p.password=:password ");//AND p.regToken=:regToken
	    q.setParameter("email", email);
	    q.setParameter("password", password);
	    //q.setParameter("reg", "true");
	    List<Person> list = q.getResultList();
	    if (list.size() == 0)
	      return null;
	    else
	      return list.get(0);
	  }
}
